package com.qfjy.project.meeting;

import com.qfjy.common.util.result.ResultCode;
import com.qfjy.common.util.result.ResultJson;
import com.qfjy.entity.po.MeetingPub;
import com.qfjy.entity.po.User;
import com.qfjy.service.MeetingGrabService;
import com.qfjy.service.MeetingPubService;
import com.qfjy.service.UserService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@Slf4j
@RequestMapping("/meetingPub")
@Api(tags = {"会议发单模块"})
public class MeetingPubController {

    @Autowired
    private MeetingPubService meetingPubService;
    @Autowired
    private UserService userService;
    @Autowired
    private MeetingGrabService meetingGrabService;
    /**
     * todo 会议的发布功能
     * @return
     */
    @RequestMapping("/addMeetingPub")
    @ResponseBody
    public ResultJson addMeetingPub(MeetingPub meetingPub) {
        int num = meetingPubService.insertSelective(meetingPub);
        if (num == -1) {
            log.info("会议发单失败{}",meetingPub);
            return new ResultJson(num, ResultCode.SUCCESS_IS_HAVE);
        } else {
            log.info("会议发单成功{}",meetingPub);
            return new ResultJson(num, ResultCode.SUCCESS);
        }
    }


    /**
     * todo 会议发布--我的会议列表
     */
    @GetMapping("/selectListMeetingPubByUid")
    @ResponseBody
    public ResultJson selectListMeetingPubByUid(@RequestParam("uid") final int uid) {
        List<MeetingPub> list = meetingPubService.selectListMeetingPubByUid(uid);
        if (list.size() == 0) {
            return new ResultJson(null, ResultCode.NOT_DATA);
        }
        // 可以通过AOP方式进行日志的管理
        return new ResultJson(list,ResultCode.SUCCESS);
    }

    /**
     * todo 会议发布--我的会议列表-选择讲者-抢单者列表
     */
    @GetMapping("/selectUserMeetingGrabByPid")
    @ResponseBody
    public ResultJson selectUserMeetingGrabByPid(@RequestParam("pid") final String pid) {
        List<User> list = userService.selectUserByMeetingGrabPid(pid);
        if (list.size()==0) {
            return new ResultJson(null, ResultCode.NOT_DATA);
        }
        return new ResultJson(list,ResultCode.SUCCESS);
    }


    @GetMapping("updateMeetingGrabChoose")
    @ResponseBody
    public Object updateMeetingGrabChoose(@RequestParam("pid") final String pid,
                                          @RequestParam("uid") final int uid){
        try {
            int num = meetingGrabService.updateMeetingGrabChoose(pid, uid);
        }catch (RuntimeException rex){
            return new ResultJson<>(null,ResultCode.FAIL);
        }catch (Exception exception){
            return new ResultJson<>(null,ResultCode.SYS_ERROR);
        }

        return new ResultJson<>(1,ResultCode.SUCCESS);
    }

    /**
     * 跳转到抢单者列表
     * @return
     */
    @GetMapping("meetingPubToGrabList")
    public String meetingPubToGrabList(@RequestParam("pid") final String pid, HttpServletRequest request){
        request.setAttribute("pid",pid);

        return "meetingPub/grabListByPid";
    }

}
